package com.health.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.health.model.BloodPressure;
import com.health.model.User;
import com.health.service.impl.BloodPressureServiceImpl;
import com.health.service.impl.UserServiceImpl;
import com.health.util.PKGenerator;

public class BloodPressureManagedBean {
	private static Log log = LogFactory.getLog(BloodPressureManagedBean.class);
			
	//分页相关,页面调用
	private int scrollerPage = 1;
	//this object is as search condition
	private BloodPressure searchVo = new BloodPressure();
	//model
	private BloodPressure pressure = new BloodPressure();
	//search result list
	private List<BloodPressure> resultList = new ArrayList<BloodPressure>();
	private BloodPressureServiceImpl pressureService = new BloodPressureServiceImpl();
	private UserServiceImpl userService = new UserServiceImpl();
	//
	private HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	//service.insert之后的返回值			1:succeed	0:fail
	private String insertFlag = "1";
	
	
	
	/**
	 * constructors
	 * */
	public BloodPressureManagedBean() {}
	
	/**
	 * forward to "pressure-list.jsp"
	 * */
	public String forBloodPressureList() {
		//每个用户(user级别/permission=0)登陆后显示血压列表
		//通过当前session得到用户信息
		User u = (User) session.getAttribute("currentLoginInfo");
		//利用userId建立p对象(查询条件)来查询出列表
		searchVo.setUserId(u.getId());
		resultList = pressureService.selectForList(searchVo);//
		//执行跳转
		return "pressure-list";
	}
	
	
	/**
	 * invoke SERVICE SELECTFORLIST
	 * */
	public void selectForList() {
		//查询条件加入当前用户的ID(session中得到)(只能查到自己的信息)
		User u = (User) session.getAttribute("currentLoginInfo");
		if(u == null) {
			log.info("No one logined!can not search pressure!");
			return;
		}
		searchVo.setUserId(u.getId());
		resultList = pressureService.selectForList(searchVo);//
		init();
		return;
	}
	
	public void selectByPK() {
		pressure = pressureService.selectByPK(pressure.getId());
		return;
	}
	
	public void insert() {
		//将当前时间转换为TimeStamp
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;//Calendar里的月份从0开始
		String monthStr = month+"";
		if(month < 10) {
			monthStr = "0" + month;
		}
		int day = c.get(Calendar.DATE);
		int hour = c.get(Calendar.HOUR);
		int min = c.get(Calendar.MINUTE);
		int sec = c.get(Calendar.SECOND);
		String timestampStr = year + "-" + monthStr + "-" + day + " " + hour + ":" + min + ":" + sec; 
		pressure.setExamTime(Timestamp.valueOf(timestampStr));
		
		User u = (User)session.getAttribute("currentLoginInfo");
		if(u == null) {
			log.warn("No user in session!can't insert pressure data!");
			return;
		}
		List<User> list = userService.selectForList(u);
		if(list.size() == 0) {
			log.warn("user search is 'NULL)'");
			return;
		}
		u = list.get(0);
		pressure.setUserId(u.getId());
		pressure.setUserName(u.getName());
		pressure.setId(PKGenerator.bloodPressureIdGenerator(pressure));
		insertFlag = pressureService.insert(pressure)+"";
		System.out.println(insertFlag);
		init();
	}
	
	public void deleteByPK() {
		int deleteFlag = pressureService.deleteByPK(pressure.getId());
		forBloodPressureList();
		init();
		return;
	}
	
	public void updateByPK() {
		int updateFlag = pressureService.updateByPK(pressure);
		init();
		return;
	}
	
	
	
	/**
	 * init objects
	 * */
	public void init() {
		searchVo = new BloodPressure();
		pressure = new BloodPressure();
		//resultList = new ArrayList<BloodPressure>();
	}
	
	
	
	/**
	 * getters and setters
	 * */
	public BloodPressure getSearchVo() {
		return searchVo;
	}

	public void setSearchVo(BloodPressure searchVo) {
		this.searchVo = searchVo;
	}

	public List<BloodPressure> getResultList() {
		return resultList;
	}

	public void setResultList(List<BloodPressure> resultList) {
		this.resultList = resultList;
	}

	public BloodPressureServiceImpl getPressureService() {
		return pressureService;
	}

	public void setPressureService(BloodPressureServiceImpl pressureService) {
		this.pressureService = pressureService;
	}

	public int getScrollerPage() {
		return scrollerPage;
	}

	public void setScrollerPage(int scrollerPage) {
		this.scrollerPage = scrollerPage;
	}

	public BloodPressure getPressure() {
		return pressure;
	}

	public void setPressure(BloodPressure pressure) {
		this.pressure = pressure;
	}

	public static Log getLog() {
		return log;
	}

	public static void setLog(Log log) {
		BloodPressureManagedBean.log = log;
	}

	public UserServiceImpl getUserService() {
		return userService;
	}

	public void setUserService(UserServiceImpl userService) {
		this.userService = userService;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public String getInsertFlag() {
		return insertFlag;
	}

	public void setInsertFlag(String insertFlag) {
		this.insertFlag = insertFlag;
	}

	
}
